Skip to content

Adds WebSocket server support#37

Open
dkoontz wants to merge 1 commit intogren-lang:mainfrom
dkoontz:websocket-server
Open

Adds WebSocket server support#37
dkoontz wants to merge 1 commit intogren-lang:mainfrom
dkoontz:websocket-server

Conversation

@dkoontz
Copy link

@dkoontz dkoontz commented Feb 13, 2026

Adds WebSocket server capabilities.

This is mostly modeled after the HttpServer module. There are tests but I haven't integrated it into a real application yet (going to work on gren-lang/browser#101 next so I have both parts to test with).

Adds the WebSocket module to the Node platform allowing Gren programs to create WebSocket servers and handle client connections.
Ok server ->
{ model = { model | server = Just server }
, command =
Stream.writeLineAsBytes "WebSocket server started on port 8085" model.stdout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify these staments with the Stream.Log module.

ws.on("message", (data, isBinary) => {
const entry = { data, isBinary };
if (waitingForMessageQueue.length > 0) {
waitingForMessageQueue.shift()(entry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo? ()(entry)?

// Each handler reads the current reference from wss on every event fire,
// so re-evaluating subscriptions updates behavior for existing connections.
ws.on("message", function (data, isBinary) {
var handlers = wss.__grenMessageHandlers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to rename wss to server and ws to client? The names are easy to mix up as it currently stands.


{-| Convert a [`ConnectionId`](WebSocketServer.ConnectionId) to an `Int`.

Useful for storing connections in a `Dict Int Connection`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just expose ConnectionId as Int instead of it being a opaque type? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants